package com.module.base.base

import android.content.Context
import android.content.Intent
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.module.base.arouter.BaseArouterUtils
import com.module.base.arouter.service.IBaseService
import com.module.base.base.loadsir.LoadSirRegister
import com.module.base.base.loadsir.LoadSirReloadListener
import com.module.base.bus.ActivityResultBus
import com.module.base.ext.activityResultFlow
import com.module.base.network.exception.ServerException
import com.module.base.network.exception.emptyException
import com.jaeger.library.StatusBarUtil
import com.kingja.loadsir.core.LoadService
import com.module.frame.app.AppStatusManager
import com.module.frame.base.BaseVmVdbActivity
import com.module.frame.ext.postEvent
import com.module.skin.SkinUtils
import com.module.skin.bean.ISkinType

/**
 *   主要用于  路由跟 、缺省图 初始化
 */
abstract class BaseVmDbActivity<VM : BaseExceptionViewModel, DB : ViewDataBinding> :
    BaseVmVdbActivity<VM, DB>(), LoadSirReloadListener {

    @JvmField
    var loadService: LoadService<Any>? = null

    @IdRes
    protected open fun getErrorViewRes(): Int {
        return 0
    }

    protected open fun setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null) //状态栏透明
        if (SkinUtils.isUse(ISkinType.IName.DEFAULT)) {
            StatusBarUtil.setLightMode(this)
        } else {
            StatusBarUtil.setDarkMode(this)
        }
    }

    @CallSuper
    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        if (AppStatusManager.getInstance().appStatus == AppStatusManager.AppStatusConstant.APP_FORCE_KILLED) {
            //界面被销毁
            val service: IBaseService = BaseArouterUtils.getBaseService()
            service.restartApp(this)
        }
        setStatusBar()
        if (isErrorView()) {
            //不要使用懒加载去初始化，因为里面有Handler().post导致初始化慢问题
            loadService = LoadSirRegister.register(this, findViewById(getErrorViewRes()))
        }
    }

    /**
     * 是否有设置缺省图
     */
    private fun isErrorView(): Boolean {
        return getErrorViewRes() != 0
    }

    override fun showLoading() {
        if (loadService != null && loadService?.currentCallback == getLoadingCallback()) {
            //如果当前是加载试图，则不需要启动加载框
            return
        }
        super.showLoading()
    }


    /**
     * 重新加载，,界面会先空白，设置成加载界面
     */
    @CallSuper
    override fun reload() {
        if (!isErrorView()) {
            return
        }
        loadService?.showCallback(getLoadingCallback())
    }

    /**
     * 刷新，保留现有的界面，展示出加载框
     */
    @CallSuper
    open fun refresh() {
        if (!isErrorView()) {
            return
        }
        showLoading()
    }

    open fun showSuccess() {
        if (isFinishing) {
            return
        }
        hideLoading()
        if (!isErrorView()) {
            return
        }
        loadService?.showSuccess()
    }

    open fun showEmpty() {
        if (isFinishing) {
            return
        }
        if (!isErrorView()) {
            return
        }
        try {
            loadService?.showWithConvertor(emptyException())
        } catch (e: Exception) {
            loadService?.showCallback(getEmptyCallback())
        }
    }

    open fun showError(exception: ServerException?) {
        if (isFinishing) {
            return
        }
        if (!isErrorView()) {
            return
        }
        try {
            loadService?.showWithConvertor(exception)
        } catch (e: Exception) {
            loadService?.showCallback(getErrorCallback())
        }
    }

    open fun getContext(): Context {
        return this
    }

    open fun getActivity(): BaseVmDbActivity<VM, DB> {
        return this
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        postEvent(activityResultFlow, ActivityResultBus(requestCode, resultCode, data))
    }
}